<?php
// +----------------------------------------------------------------------
// | Scallion 
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.scallion.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liuxg < 420695648@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use think\queue\Job;
class send_message {
      
	/**
	* fire方法是消息队列默认调用的方法
	* @param Job            $job      当前的任务对象
	* @param array|mixed    $data     发布任务时自定义的数据
	*/
	public function fire(Job $job,$data){
		// 如有必要,可以根据业务需求和数据库中的最新数据,判断该任务是否仍有必要执行.
		$isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
		if(!$isJobStillNeedToBeDone){
			$job->delete();
			return;
		}

		$isJobDone = $this->doHelloJob($data);

		if ($isJobDone) {
			//如果任务执行成功， 记得删除任务
			$job->delete();
			// print("<info>Hello Job has been done and deleted"."</info>\n");
		}
		else{
			if ($job->attempts() > 3) {
				//通过这个方法可以检查这个任务已经重试了几次了
				print("<warn>Hello Job has been retried more than 3 times!"."</warn>\n");
				$job->delete();
				// 也可以重新发布这个任务
				//print("<info>Hello Job will be availabe again after 2s."."</info>\n");
				//$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
			}
		}
	}
	/**
	* 有些消息在到达消费者时,可能已经不再需要执行了
	* @param array|mixed    $data     发布任务时自定义的数据
	* @return boolean                 任务执行的结果
	*/
	private function checkDatabaseToSeeIfJobNeedToBeDone($data){
		return true;
	}

	/**
	* 根据消息中的数据进行实际的业务处理
	* @param array|mixed    $data     发布任务时自定义的数据
	* @return boolean                 任务执行的结果
	*/
	private function doHelloJob($data) {
		// 根据消息中的数据进行实际的业务处理...
		$msg['touser'] = $data['openid'];
		// $msg['template_id'] = 'sn5bccCCCZYagmtEguTcg7_BqIrR2IJruXWkSVn-rxs';//测试
		$msg['template_id'] = 'bltJzJNl_iKnVqczEA4U_69_ObkOZaQGpQvmJjK-tL0';
		$msg['url'] = url('portal/wxview/wx_index',['state'=>'user_center'],'html',true);
		$msg['data']['first']['value'] = date('Y年m月d日');
		$msg['data']['first']['color'] = '#173177';
		$msg['data']['keyword1']['value'] = $data['user_nickname'];
		$msg['data']['keyword1']['color'] = '#173177';
		$msg['data']['keyword2']['value'] = $data['mobile'];
		$msg['data']['keyword2']['color'] = '#173177';
		$msg['data']['keyword3']['value'] = date('Y年m月d日 H:i:s');
		$msg['data']['keyword3']['color'] = '#173177';
		$msg['data']['remark']['value'] = '您已成功绑定微信！';
		$msg['data']['remark']['color'] = '#173177';
		$wechat = new \wechatcontroller;
		$data = $wechat->wx_send_template_message($msg);
		// print("<info>Hello Job Started. job Data is: ".$abc."</info> \n");
		// print("<info>Hello Job Started. job Data is: ".var_export($data,true)."</info> \n");
		// print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') ."</info> \n");
		// print("<info>Hello Job is Done!"."</info> \n");
// dump($data);
		return true;
	}
}